const mongoose = require('mongoose');
const logger=require('../log/log_config');
const moment = require("moment");
const md5=require("md5");
var Merchant = mongoose.model('merchant');
var CopyFile = require('../util/copy');
var fs = require('fs');
const MerchantJms=require('../jms/merchant_jms_producer');
const B=require('../config/base');
const request=require('request');
var b=new B();
var base=b.img;
var jms=b.jms;


//repect & login & logout
exports.merchantAction = function (req, res) {
    var three = JSON.stringify(req.query);

    if (three == "{}") {
        req.session=null;
        res.redirect("./merchantLogin1.html");
    } else {
        var password = req.query.password;

        if (!password) {
            Merchant.findOne({account: req.query.account}, function (err, merchant) {
                if (!err && merchant) {
                    res.send("y");
                } else {
                    res.send("---------");
                }
            });

        } else {
                password=md5(password);
            Merchant.findOne({account: req.query.account}, function (err, merchant) {
                if(merchant==null){

                    res.send("false");
                }else
                 if (!err && password == merchant.password) {
                    logger.log("info", "login time:%s", moment().format("LLLL"), "User Message:%s", JSON.stringify(merchant), {});
                    req.session.mid = merchant._id;
                    req.session.account = merchant.account;
                    request(jms.merchantStatue+merchant._id,function(err,response,body){
                        if(!err&&body!=null){
                            var s = JSON.parse(body).statue;
                            var result = "";
                            switch (s) {
                                case -2:
                                    result = "-2";
                                    break;
                                case -1:
                                    result = "-1";
                                    break;
                                case 0:
                                    result = "0";
                                    break;
                                case 1:
                                    result = "1";
                                    break;
                                default:
                                    result = "false";
                                    break;
                            }
                            res.json(result);
                        }else{
                            res.send("false");
                        }
                    });


                } else {
                    if (!err) {
                        res.send("false");
                    } else {
                        res.send("error");
                    }

                }
            });

        }

    }


}

exports.merchantRegist = function (req, res) {
    // console.log(req.body);
    req.body.star = 5;
    req.body.orderNumber = 0;
    req.body.statue = 0;

    if (JSON.stringify(req.body) != "{}" && req.body.account && req.body.password) {
        req.body.password=md5(req.body.password);
        var m = new Merchant(req.body);
        m.save({}, function (err, data) {

            if (err) {
                res.json(err);
            } else {
                var mer=new MerchantJms();
                mer.sendM(JSON.stringify(data));
                res.json(data);
            }
        });
    } else {
        res.redirect("./merchantRegist.html");
    }

}

exports.updateMerchantMsg = function (req, res) {
    Merchant.findOneAndUpdate({_id: req.body._id}, req.body, function (err, merchant) {
        if (!err) {
            Merchant.findOne({_id:merchant._id},function(error,mer){
                if(!error){
                    res.json(mer);
                }else{
                    res.json(error);
                }

            });

        } else {
            res.json(err);
        }

    });
}


exports.loadMerchantMsg = function (req, res) {
    var mid=req.session.mid;
    // console.log("session.mid="+req.session.mid);
    Merchant.findOne({_id: mid}, function (err, merchant) {
        if (!err) {
            res.json(merchant);
        } else {
            res.json(err);
        }
    });
}

exports.fileUpload = function (req, res) {
    var realName = req.files.file.path;
    fs.rename(realName, base.img.savePath + req.files.file.name, function (err) {
        if (err) {
            throw err;
        }
    });
    CopyFile.exists(base.img.tempFile, base.img.targetFile, CopyFile.copy);
    res.json({size: 0});
}




